<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>事件管理</title>
<jsp:include page="../inc.jsp" flush="false"></jsp:include>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/plug-in/custom/form.js"
        charset="utf-8"></script>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/plug-in/custom/dateTime.js"
        charset="utf-8"></script>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/plug-in/custom/selectorCrm/businessOpportunitiesSel.js"
        charset="utf-8"></script>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/plug-in/custom/selectorCrm/contractsSel.js"
        charset="utf-8"></script>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/plug-in/custom/selectorCrm/customersSel.js"
        charset="utf-8"></script>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/plug-in/custom/selectorCrm/potentialCustomersSel.js"
        charset="utf-8"></script>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/app/select/contactSel.js"
        charset="utf-8"></script>
<script type="text/javascript">
Ext.onReady(function () {
    Ext.QuickTips.init();
    createGrid();
    createLayout();
    createForm();
    form = Ext.create('programActivitiesForm');

});
var gridEl = 'programActivitiesGrid';
var grid, form, selModel, toolbar, searchFlds;
var win,entityType='<%= ((request.getParameter("entityType")==null)?"": request.getParameter("entityType"))%>';

function createWindows() {
    if (win == null) {
        win = new Ext.Window({
            title: '新增',
            iconCls: 'icon-win',
            autoHeight: true,
            width: 600,
            height: 500,
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/programActivitiesController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    win.hide();
                                    grid.getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    },
                    id: "btnSave"
                },
                {
                    text: "关闭",
                    id: "btnClose",
                    handler: function () {
                        win.hide();
                    }
                }
            ],
            layout: 'fit',
            plain: false,
            closable: false,
            modal: true,
            items: [ form ]
        });
    }
    win.show();
}
var customerWin,contractsWin,businessOpportunitiesWin,potentialCustomersWin;
function    OnEntityType(){
    if(entityType==''){
        Ext.Msg.alert('提示','请选择相关选项');
        return;
    }
    switch (entityType) {
	    case'kh':
	        if(!customerWin){
	            customerWin= new customersSel();
	            customerWin.on('onSelectItem', function(data) {
                    form.getForm().findField("customersId").setValue(data.id);
	                form.getForm().findField("entityID").setValue(data.id);
	                form.getForm().findField("entityIDT").setValue(data.company);
	            }, this);
	        }
	        customerWin.show();
	        break;
        case'ywjh':
            if(!businessOpportunitiesWin){
                businessOpportunitiesWin= new businessOpportunitiesSel();
                businessOpportunitiesWin.on('onSelectItem', function(data) {
                    form.getForm().findField("customersId").setValue(data.customers.id);
                    form.getForm().findField("entityID").setValue(data.id);
                    form.getForm().findField("entityIDT").setValue(data.name);
                }, this);
            }
            businessOpportunitiesWin.show();
            break;
        case'ht':
            if(!contractsWin){
                contractsWin= new contractsSel();
                contractsWin.on('onSelectItem', function(data) {
                    form.getForm().findField("customersId").setValue(data.customers.id);
                    form.getForm().findField("entityID").setValue(data.id);
                    form.getForm().findField("entityIDT").setValue(data.name);
                }, this);
            }
            contractsWin.show();
            break;
        case'qzkh':
            if(!potentialCustomersWin){
                potentialCustomersWin= new potentialCustomersSel();
                potentialCustomersWin.on('onSelectItem', function(data) {
                    form.getForm().findField("entityID").setValue(data.id);
                    form.getForm().findField("entityIDT").setValue(data.company);
                }, this);
            }
            potentialCustomersWin.show();
            break;
    }
    form.getForm().findField("contactIdT").setValue('');
    form.getForm().findField("contactId").setValue('');
}

function contactSel(){
     var contactSel= Ext.create('App.select.contactSel',{customersId:form.getForm().findField("customersId").getValue()});
    contactSel.on('onSelectItem', function(data) {
        form.getForm().findField("contactId").setValue(data.id);
        form.getForm().findField("contactIdT").setValue(data.name);
    }, this);
    contactSel.show();
}

// 创建form表单
function createForm() {
    Ext.define('programActivitiesForm', {
        extend: 'Ext.form.Panel',
        fields: new Ext.util.MixedCollection(),
        initComponent: function () {
            this.fields.add('id', new Ext.form.Hidden({
                name: 'id',
                dataIndex: 'id'
            }));
            this.fields.add('deleteFlag', new Ext.form.Hidden({
                name: 'deleteFlag',
                dataIndex: 'deleteFlag'
            }));  this.fields.add('customersId', new Ext.form.Hidden({
                name: 'customersId',
                dataIndex: 'customersId'
            }));
            this.fields.add('entityType', new comboBox({
                name: 'entityType',
                dataIndex: 'entityType',
                fieldLabel: '相关项',
                labelAlign: 'right',
                codeName: '活动相关项',
                queryMode: 'local',
                typeAhead: false,
                forceSelection: true,
                loadingText: '正在搜索...',
                allowBlank: true,
                hideTrigger: false,
                listeners: {
                    select: function (combo, value, option) {
                        form.getForm().findField("contactIdT").setDisabled(false);
                        form.getForm().findField("contactIdT").setDisabled(false);
                        form.getForm().findField("entityID").setValue("");
                        form.getForm().findField("entityIDT").setValue("");
                        entityType = value[0].raw.itemValue ;
                        if(entityType=='qzkh')
                        {
                            form.getForm().findField("contactIdT").setDisabled(true);
                            form.getForm().findField("contactIdT").setDisabled(true);
                        }
                        else{
                            form.getForm().findField("contactIdT").setDisabled(true);
                            form.getForm().findField("contactIdT").setDisabled(true);
                        }

                    }}
            }));
            this.fields.add('entityID', new Ext.form.Hidden({
                name: 'entityID',
                dataIndex: 'entityID'
            }));
            this.fields.add('entityIDT', new Ext.form.TextField({
                name: 'entityIDT',
                dataIndex: 'entityIDT',
                fieldLabel: '相关项值',
                disabled: true,
                allowBlank: true,
                width: 230
            }));
            this.fields.add('btnEntityID', new Ext.Button({
                name: 'btnEntityID',
                dataIndex: 'btnEntityID',
                iconCls: 'searchIcon',
                handler: function () {
                    OnEntityType();
                },
                width: 25
            }));
            this.fields.add('title', new Ext.form.TextField({
                name: 'title',
                dataIndex: 'title',
                fieldLabel: '主题',
                allowBlank: true
            }));
            this.fields.add('contactId', new Ext.form.Hidden({
                name: 'contactId',
                dataIndex: 'contactId'
            }));
            this.fields.add('contactIdT', new Ext.form.TextField({
                name: 'contactIdT',
                dataIndex: 'contactIdT',
                fieldLabel: '联系人',
                allowBlank: true,
                width: 230
            }));
            this.fields.add('btnContactId', new Ext.Button({
                name: 'btnContactId',
                dataIndex: 'btnContactId',
                iconCls: 'searchIcon',
                handler: function () {
                    if(entityType!='qzkh'){
                        contactSel();
                    }
                },
                width: 25
            }));
            this.fields.add('startDate', new dateTime({
                name: 'startDate',
                dataIndex: 'startDate',
                format: 'Y-m-d H:i:s',
                fieldLabel: '开始日期',
                allowBlank: true
            }));
            this.fields.add('startTime', new Ext.form.TextField({
                name: 'startTime',
                dataIndex: 'startTime',
                fieldLabel: '开始时间',
                allowBlank: true
            }));
            this.fields.add('endDate', new dateTime({
                name: 'endDate',
                dataIndex: 'endDate',
                format: 'Y-m-d H:i:s',
                fieldLabel: '结束日期',
                allowBlank: true
            }));
            this.fields.add('endTime', new Ext.form.TextField({
                name: 'endTime',
                dataIndex: 'endTime',
                fieldLabel: '结束时间',
                allowBlank: true
            }));
            this.fields.add('telephone', new Ext.form.TextField({
                name: 'telephone',
                dataIndex: 'telephone',
                fieldLabel: '电话',
                allowBlank: true
            }));
            this.fields.add('email', new Ext.form.TextField({
                name: 'email',
                dataIndex: 'email',
                fieldLabel: '电子邮件',
                allowBlank: true
            }));
            this.fields.add('remark', new Ext.form.TextField({
                name: 'remark',
                dataIndex: 'remark',
                fieldLabel: '备注',
                allowBlank: true
            }));
            this.fields.add('warnState', new Ext.form.NumberField({
                name: 'warnState',
                dataIndex: 'warnState',
                allowDecimals: false,
                decimalPrecision: null,
                fieldLabel: '提醒',
                allowBlank: true
            }));
            this.fields.add('warnDate', new dateTime({
                name: 'warnDate',
                dataIndex: 'warnDate',
                format: 'Y-m-d H:i:s',
                fieldLabel: '提醒日期',
                allowBlank: true
            }));
            this.fields.add('isSendSMS', new Ext.form.NumberField({
                name: 'isSendSMS',
                dataIndex: 'isSendSMS',
                allowDecimals: false,
                decimalPrecision: null,
                fieldLabel: '是否开启短信提醒',
                allowBlank: true
            }));
            this.fields.add('sendPhone', new Ext.form.TextField({
                name: 'sendPhone',
                dataIndex: 'sendPhone',
                fieldLabel: '收件手机',
                allowBlank: true
            }));


            this.items = [ this.fields.get('id'), this.fields.get('deleteFlag'), {
                xtype: 'fieldset',
                title: '事件信息',
                collapsible: true,
                defaults: {
                    labelWidth: 89,
                    anchor: '100%',
                    layout: {
                        type: 'hbox',
                        defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                    }
                },
                items: [
                    {
                        xtype: 'container',
                        layout: 'hbox',
                        margin: '0 0 5 0',
                        defaults: {
                            flex: 1,
                            hideLabel: true
                        }, items: [ this.fields.get('entityType'),
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {

                                hideLabel: true
                            }, items: [this.fields.get('entityIDT'), this.fields.get('btnEntityID'),
                            this.fields.get('entityID')]}]},
                    {
                        xtype: 'container',
                        layout: 'hbox',
                        margin: '0 0 5 0',
                        defaults: {
                            flex: 1,
                            hideLabel: true
                        }, items: [ this.fields.get('title'),
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {

                                hideLabel: true
                            }, items: [this.fields.get('contactIdT'), this.fields.get('btnContactId'),
                            this.fields.get('contactId')]}]},
                    {
                        xtype: 'container',
                        layout: 'hbox',
                        margin: '0 0 5 0',
                        defaults: {
                            flex: 1,
                            hideLabel: true
                        }, items: [ this.fields.get('startDate'), this.fields.get('startTime')]},
                    {
                        xtype: 'container',
                        layout: 'hbox',
                        margin: '0 0 5 0',
                        defaults: {
                            flex: 1,
                            hideLabel: true
                        }, items: [ this.fields.get('endDate'), this.fields.get('endTime')]}
                ]}, {
                xtype: 'fieldset',
                title: '其他信息',
                collapsible: true,
                defaults: {
                    labelWidth: 89,
                    anchor: '100%',
                    layout: {
                        type: 'hbox',
                        defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                    }
                },
                items: [
                    {
                        xtype: 'container',
                        layout: 'hbox',
                        margin: '0 0 5 0',
                        defaults: {
                            flex: 1,
                            hideLabel: true
                        }, items: [ this.fields.get('telephone'), this.fields.get('email')]},
                    this.fields.get('remark')
                ]},
                {
                    xtype: 'fieldset',
                    title: '提醒',
                    collapsible: true,
                    defaults: {
                        labelWidth: 89,
                        anchor: '100%',
                        layout: {
                            type: 'hbox',
                            defaultMargins: {top: 0, right: 5, bottom: 0, left: 0}
                        }
                    },
                    items: [
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1,
                                hideLabel: true
                            }, items: [ this.fields.get('warnState'), this.fields.get('warnDate')]},
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            margin: '0 0 5 0',
                            defaults: {
                                flex: 1,
                                hideLabel: true
                            }, items: [ this.fields.get('isSendSMS'), this.fields.get('sendPhone')]}
                    ]} ];
            this.xtype = 'form';
            this.frame = false;
            this.border = true;
            this.split = true;
            this.autoScroll = true,
                    this.bodyPadding = 10,
                    this.bodyBorder = true;
            this.fieldDefaults = {
                labelWidth: 75,
                labelAlign: 'right',
                msgTarget: 'side'
            };
            this.defaults = {
                anchor: '100%'
            };
            this.callParent(arguments);
        },
        onRender: function () {
            this.callParent(arguments);
        },
        newDataRecord: function () {
            this.getForm().reset();
        }
    });
}


// 创建新的记录，初始化表单
function EditRecord() {
    var sm = grid.getSelectionModel();
    var records = sm.getSelection();
    if (records.length <= 0) {
        Ext.Msg.alert('错误', "未选中记录，不能修改！");
        return;
    }
    var data = records[0].data;
    form.getForm().loadRecord(records[0]);
    createWindows();
}

function createGrid() {
    searchFlds = new Ext.util.MixedCollection();

    grid = Ext.create('gridView', {
        remoteSort: true,
        gridId: gridEl,
        checkboxSelectionModel: true,
        searchFlds: searchFlds,
        entityClassName: 'com.rover.crm.entity.ProgramActivities',
        queryUrl: 'programActivitiesController/query.action',
        deleteUrl: 'programActivitiesController/delete.action',
        recordPk: [ "id" ]
    });
    grid.getStore().on('beforeload', function (store, options) {
        store.proxy.extraParams.entityID = '<%=request.getParameter("entityID")%>';
        store.proxy.extraParams.entityType = entityType;
    }, this, true);
}
// 创建layout
function createLayout() {
    toolbar = new Ext.Toolbar({
        enableOverflow: true,
        items: [
            {
                text: "新增",
                iconCls: 'addIcon',
                id: "tlb_NEW",
                handler: function () {
                    form.newDataRecord();
                    createWindows();
                    win.setTitle("新增");
                }
            },
            '-',
            {
                text: "编辑",
                iconCls: 'editIcon',
                id: "tlb_EDIT",
                handler: function () {
                    EditRecord();
                    win.setTitle("编辑");
                }
            },
            '-',
            {
                text: "删除",
                iconCls: 'deleteIcon',
                id: "tlb_DELETE",
                handler: function () {
                    grid.deleteRecord();
                }
            },
            "->",
            ,
            {
                text: "搜索",
                iconCls: 'searchIcon',
                handler: function () {
                    grid.load();
                }
            }
        ]
    });
    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [
            {
                layout: 'fit',
                region: 'north',
                border: false,
                items: [ toolbar ]
            },
            {
                layout: 'border',
                region: 'center',
                border: false,
                items: [ grid ]
            }
        ]
    });
    viewport.doLayout();
}
</script>
</head>
<body>
</body>
</html>